package com.example.pluto.common.utils;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSON;
import com.example.pluto.user.entity.PlutoUser;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.Country;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;

/**
 *@Author Mr.Yang
 *@Date 2025/1/10
 *@Description TODO
 */
public class Test {

//    public static void main(String[] args) throws IOException, GeoIp2Exception {
//         InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("GeoLite2-City.mmdb");
//// This reader object should be reused across lookups as creation of it is
//// expensive.
//        DatabaseReader reader = new DatabaseReader.Builder(inputStream).build();
//
//// If you want to use caching at the cost of a small (~2MB) memory overhead:
//// new DatabaseReader.Builder(file).withCache(new CHMCache()).build();
//
//        InetAddress ipAddress = InetAddress.getByName("116.179.37.4");
//
//        CityResponse response = reader.city(ipAddress);
//
//        Country country = response.getCountry();
//        System.out.println(country.getIsoCode());
//
//        System.out.println(response.getCity());
//        System.out.println(response.getCity().getName());
//
//    }

    public static void main(String[] args) {
        try {
            // 解析 IPv4/IPv6 地址
            InetAddress ip = InetAddress.getByName("192.168.1.1");

            System.out.println("Host Name: " + ip.getHostName());
            System.out.println("IP Address: " + ip.getHostAddress());
            System.out.println("Is IPv4: " + (ip.getAddress().length == 4));
            System.out.println("Is IPv6: " + (ip.getAddress().length == 16));

        } catch (UnknownHostException e) {
            System.out.println("Invalid IP format");
        }
    }

         public static void parse(String ip) throws IllegalArgumentException {
            String[] octets = ip.split("\\.");
            if (octets.length != 4) {
                throw new IllegalArgumentException("Invalid IPv4 format");
            }

            for (String octet : octets) {
                int value = Integer.parseInt(octet);
                if (value < 0 || value > 255) {
                    throw new IllegalArgumentException("Octet out of range");
                }
                // 处理前导零校验
                if (octet.length() > 1 && octet.startsWith("0")) {
                    throw new IllegalArgumentException("Leading zeros not allowed");
                }
            }
            System.out.println("Valid IPv4 address");
        }


}
